<?php

class MutuelleController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {

        $mutuelle = new Application_Model_DbTable_Mutuelles();

        //Zend_Debug::dump($mutuelle->getMetadataCache());
        //exit;

        $paginator = Zend_Paginator::factory($mutuelle->getAllMutuelle());
        $paginator->setItemCountPerPage(6);
        $paginator->setCurrentPageNumber($this->getRequest()->getParam('page'));
        $this->view->mutuelle = $paginator;
    }

    public function addAction() {

        $form = new Application_Form_Mutuelle('Ajouter', 'Nouvelle');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
            if ($form->isValid($data)) {
                $mutuelle = new Application_Model_DbTable_Mutuelles();
                $mutuelle->addMutuelle($form->getValidValues($data));
                $this->_helper->redirector('index');
            } else {
                $form->populate($data);
            }
        }
    }

    public function editAction() {
        $form = new Application_Form_Mutuelle('Enregistrer', 'Edition');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            if ($form->isValid($data)) {
                //Zend_Debug::dump($form->getValidValues($data));
                $mutuelle = new Application_Model_DbTable_Mutuelles();
                $mutuelle->editMutuelle($form->getValidValues($data));
                $this->_helper->redirector('index');
            } else {
                $form->populate($data);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $mutuelle = new Application_Model_DbTable_Mutuelles();
                // Zend_Debug::dump($patient->getPatient($id));
                $form->populate($mutuelle->getMutuelle($id));
            }
        }
    }

}

